package com.cai.servlet.common.user;

import com.alibaba.fastjson.JSONObject;
import com.cai.dao.DetailedTopicDao;
import com.cai.dao.impl.DetailedTopicDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @author RuoLi
 * 用户验证答案
 */
@WebServlet(name = "ServletCheckTopicAnswer", value = "/common/CheckTopicAnswer")
public class ServletCheckTopicAnswer extends HttpServlet {
    final String chooseTopic = "选择题";
    final String blanksTopic = "填空题";
    final String judgmentTopic = "判断题";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String detailedId = req.getParameter("detailedTopicId");
        String topicType = req.getParameter("topicType");
        DetailedTopicDao detailedTopicDao = new DetailedTopicDaoImpl();
        int count = 0;
        if (chooseTopic.equals(topicType)) {
            //选择题
            String radioAnswer = req.getParameter("detailedTopicRadioAnswer");
            //验证答案
            count = detailedTopicDao.checkUserDetailedTopicAnswer(Integer.parseInt(detailedId), radioAnswer);
        } else if (blanksTopic.equals(topicType)) {
            //填空题
            String inputAnswer = req.getParameter("detailedTopicInputAnswer");
            count=detailedTopicDao.checkUserDetailedTopicAnswer(Integer.parseInt(detailedId),inputAnswer);
        } else if (judgmentTopic.equals(topicType)) {
            //判断题
            String inputAnswer = req.getParameter("detailedTopicCheckAnswer");
            count=detailedTopicDao.checkUserDetailedTopicAnswer(Integer.parseInt(detailedId),inputAnswer);
        }
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("status",count);
        resp.getWriter().write(jsonObject.toString());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
